/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import DAL.CarBookBean;
import java.io.*;
import java.util.Date;

import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;

/**
 *
 * @author Mr-aUgust
 */
@WebServlet(name = "ControllerCancelCarBook", urlPatterns = {"/ControllerCancelCarBook"})
public class ControllerCancelCarBook extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        CarBookBean carbb=new CarBookBean();
        
        HttpSession session=request.getSession(true);
        if(request.getParameter("cancel")!=null)
        {
            if(request.getParameter("cancel").equals("Cancel"))
            {
                int book=Integer.parseInt(request.getParameter("bookingID"));
                
                String user=session.getAttribute("User").toString();
                int day=carbb.getDay(user,book);
                float tatolmoney=carbb.getMoney(user,book);
                out.println(tatolmoney);
                float tong=0;
                if(day<0)
                {
                out.println("nho hon 0 ko duoc phep tra xe");
                }
                else if(day==1)
                {
                    tong=((tatolmoney*75)/100);
                        //out.println("truoc 1 ngay 75% "+tong);
                }
                else if(day==2)
                {
                    tong=((tatolmoney*80)/100);
                   // out.println("truoc 2 ngay 80% "+tong);
                }
                else if(day==3)
                {
                    tong=((tatolmoney*85)/100);
                   // out.println("truoc 3 ngay 85% "+tong);
                }
                else if(day==4)
                {
                    tong=((tatolmoney*90)/100);
                    //out.println("truoc 4 ngay 90% "+tong);
                }  
                else
                {
                    tong=((tatolmoney*95)/100);
                    //out.println("truoc 5 or more than ngay 95% "+tong);
                }
                session.setAttribute("Bookid",book);
                session.setAttribute("Refund",tong);
                response.sendRedirect("index.jsp?page=DetailCancelCar");
            }
        }
       if(request.getParameter("comfirm")!=null)
        {
            if(request.getParameter("comfirm").equals("Comfirm"))
            {
                int bookid=Integer.parseInt(request.getParameter("bookid"));
                float refund=Float.parseFloat(request.getParameter("refund"));
                Date d=new Date();
                int day=d.getDate();
                int month=d.getMonth();
                int year=1900+d.getYear();
               // out.println(day+":"+month+":"+year);
               String dateCancel=month+"/"+day+"/"+year;
                boolean rs=carbb.UpCancelCarBook(bookid,refund,dateCancel);
                if(rs==true)
                {
                    carbb.UpdateQuantity(bookid,"Cancel");
                    out.println("<script>alert('Cancel Car rental success!');window.location='index.jsp?page=MyCarBook';</script>");
                }
                else
                {
                    //System.out.println("<a href='index.jsp?page=MyCarBook'>Cancel car fail back...!</a>");
                    out.println("<script>alert('Cancel car rental fail!');javascript:window.history.back(-1);</script>");
                }
            }
       }
         if(request.getParameter("notcancel")!=null)
        {
            if(request.getParameter("notcancel").equals("Not Cancel"))
            {
                session.removeAttribute("Bookid");
                session.removeAttribute("Refund");
                response.sendRedirect("index.jsp?page=MyCarBook");
            }
         }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
